package com.yicong.ants.manager;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.adapter.StatefulBindMultiQuickAdapter;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.interfaces.BaseStateView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.bean.base.BaseBean;
import com.yicong.ants.bean.base.PageRespBean;

/**
 * @author cchao
 * @version 2019-08-08.
 */
public class AdapterHelper {

    public static void solveFailResp(RecyclerView recyclerView, int page, BaseBean respBean) {
        if (!respBean.isCodeFail()) {
            return;
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        Context context = recyclerView.getContext();
        BaseStateView baseView = (BaseStateView) context;

        baseView.showToast(respBean.getMsg());
        if (page != 1) {
            if (adapter instanceof BaseQuickAdapter) {
                if (respBean.isCodeEmpty()) {
                    ((BaseQuickAdapter) adapter).loadMoreEnd();
                } else {
                    ((BaseQuickAdapter) adapter).loadMoreFail();
                }
            }
            return;
        }

        // 第一页
        if (adapter instanceof StatefulBindQuickAdapter) {
            if (respBean.isCodeEmpty()) {
                ((StatefulBindQuickAdapter) adapter).setViewState(MultiStateView.VIEW_STATE_EMPTY);
            } else {
                ((StatefulBindQuickAdapter) adapter).setViewState(MultiStateView.VIEW_STATE_ERROR);
            }
        } else {
            if (respBean.isCodeEmpty()) {
                baseView.switchView(BaseStateView.EMPTY);
            } else {
                baseView.switchView(BaseStateView.NET_ERROR);
            }
        }
    }

    /**
     * 将所有 第一页出错或者响应为空 都切换到 empty 页面，
     *
     * @param recyclerView recyclerView
     * @param page         page
     * @param respBean     respBean
     */
    public static void solvePageBean(RecyclerView recyclerView, int page, PageRespBean respBean) {
        Context context = recyclerView.getContext();
        if (!(recyclerView.getAdapter() instanceof StatefulBindQuickAdapter)) {
            UiHelper.showToast("adapter must instanceof StatefulBindQuickAdapter");
            return;
        }
        StatefulBindQuickAdapter adapter = (StatefulBindQuickAdapter) recyclerView.getAdapter();

        // 成功就处理数据
        if (respBean.isCodeSuc()) {
            // 成功，但是数据为空
            if (respBean.getData() == null || respBean.getList() == null || respBean.getPageInfo() == null) {
                adapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                return;
            }
            adapter.solvePageData(respBean.getList(), page, respBean.getPageInfo().getCountPage());
            return;
        }
        // 失败并且不是数据为空  就弹出提示，并界面显示空
        if (!respBean.isCodeEmpty()) {
            if (context instanceof BaseStateView) {
                ((BaseStateView) context).showToast(respBean.getMsg());
            } else {
                UiHelper.showToast(respBean.getMsg());
            }
        }
        if (page == 1) {
            adapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
        } else {
            adapter.loadMoreFail();
        }
    }

    /**
     * 将所有 第一页出错或者响应为空 都切换到 empty 页面，
     *
     * @param recyclerView recyclerView
     * @param page         page
     * @param respBean     respBean
     */
    public static void solveMultiItemPageBean(RecyclerView recyclerView, int page, PageRespBean respBean) {
        Context context = recyclerView.getContext();
        if (!(recyclerView.getAdapter() instanceof StatefulBindMultiQuickAdapter)) {
            UiHelper.showToast("adapter must instanceof StatefulBindMultiQuickAdapter");
            return;
        }
        StatefulBindMultiQuickAdapter adapter = (StatefulBindMultiQuickAdapter) recyclerView.getAdapter();

        // 成功就处理数据
        if (respBean.isCodeSuc()) {
            // 成功，但是数据为空
            if (respBean.getData() == null || respBean.getList() == null || respBean.getPageInfo() == null) {
                adapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                return;
            }
            adapter.solvePageData(respBean.getList(), page, respBean.getPageInfo().getCountPage());
            return;
        }
        // 失败并且不是数据为空  就弹出提示，并界面显示空
        if (!respBean.isCodeEmpty()) {
            if (context instanceof BaseStateView) {
                ((BaseStateView) context).showToast(respBean.getMsg());
            } else {
                UiHelper.showToast(respBean.getMsg());
            }
        }
        if (page == 1) {
            adapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
        } else {
            adapter.loadMoreFail();
        }
    }

    public static RecyclerView.ItemDecoration getVerticalDividerItemDecoration(int bottom, int firstTop) {
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int adapterPosition = parent.getChildAdapterPosition(view);
                outRect.bottom = UiHelper.dp2px(bottom);
                outRect.top = UiHelper.dp2px(0);
                if (adapterPosition == 0) {
                    outRect.top = UiHelper.dp2px(firstTop);
                }
            }
        };
    }
}
